<?php

namespace IMT\ReportingBundle\Controller;

use IMT\Plugins\ParticipantsBundle\Entity\ParticipantEvent;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller {
	public function indexAction($mobile = false) {
		
		$em = $this->getDoctrine ()->getEntityManager ();
		$securityContext = $this->get ( 'security.context' );
		$user = $securityContext->getToken ()->getUser ();
		$request = $this->getRequest ();
		
		$accounts = $user->getAccounts ();
		
		$attachments = $user->getAttachments ();
		
		$treadManager = $this->container->get ( 'fos_comment.manager.thread' );
		$commentManager = $this->container->get ( 'fos_comment.manager.comment' );
		
		$numberEvents = 0;
		$numberParticipants = 0;
		$numberComments = 0;
		$numberCall = 0;
		$numberEvent = 0;
		$numberTask = 0;
		$numberInvited = 0;
		$numberDontKnow = 0;
		$numberParticipating = 0;
		$numberCanceled = 0;
		
		$numberCallPastTime = 0;
		$numberEventPastTime = 0;
		$numberTaskPastTime = 0;
		
		$nbJour = 0;
		
		$totalTime = 0;
		
		$jourEvent = array('1' => 0, '2' => 0,'3' => 0,'4' => 0,'5' => 0,'6' => 0, '7' => 0);
		
		$typePost = "";
		if (isset ( $_POST ["imt_event_form_general_account"] ) and ! empty ( $_POST ["imt_event_form_general_account"] )) {
			$typePost = $_POST ["imt_event_form_general_account"];
		}
		
		$dateDebutPost = $request->get ( "datedebut" );
		$dateFinPost = $request->get ( "datefin" );
		
		if(empty($dateDebutPost)){
			$dateDebutPost = "01/06/2013";
		}
		
		if(empty($dateFinPost)){
			$dateFinPost = "30/06/2013";
		}
		
		$arrayJour = array();
		$arrayJourComment = array();
		
		$arrayComment = array();
		$arrayFichier = array();
		
		// exit;
		foreach ( $accounts as $account ) {
			
			$events = $account->getEvents ();

			foreach ( $events as $event ) {
				
				if ($event->getType () == $typePost || empty ( $typePost )) {
					
					$dateDebutPostR = str_replace ( "/", "-", $dateDebutPost );
					$dateDebutPostF = new \DateTime ( $dateDebutPostR );
					$dateDebutPostTrait = new \DateTime ( $dateDebutPostR );
					$dateFinPostR = str_replace ( "/", "-", $dateFinPost );
					$dateFinPostF = new \DateTime ( $dateFinPostR );
					$dateFinPostTrait = new \DateTime ( $dateFinPostR );
					$dateFinPostF->modify('+1 day');
					

					if (($event->getDate()->format ('U') >= $dateDebutPostF->format('U') and $event->getDateFin()->format ('U') <= $dateFinPostF->format('U')) or ($event->getDate()->format ('U') <= $dateDebutPostF->format('U') and $event->getDateFin()->format ('U') >= $dateDebutPostF->format('U')  ) or ($event->getDate()->format ('U') >= $dateDebutPostF->format('U') and $event->getDate()->format ('U') <= $dateFinPostF->format('U')))  {
						

						// date
						$dateDebut = $event->getDate ();
						$dateFin = $event->getDateFin ();
						
						// type
						
						$type = $event->getType ();
						
						// jour 
						
						$jour = $event->getDate()->format ( 'N' );
						$jourEvent[$jour] +=1;
						
						
						//nb Jour
						
						$nbjour = round(($dateFinPostTrait->format( 'U' ) - $dateDebutPostTrait->format( 'U' ))/86400);
						$jourAdd = clone $dateDebutPostTrait;
						
						if($nbjour > 1){
							for($i = 0; $i <= $nbjour ; $i++){
								if($i != 0){
									$jourAdd->modify("+1 day");
								}else
									$jourAdd = $dateDebutPostTrait;
						
								$jourAddPlus = clone $jourAdd;
								$jourAddPlus->modify("+1 day");
								
								//var_dump($event->getTitle());
								//var_dump($jourAdd->format("d/m/Y")." || ".$jourAddPlus->format("d/m/Y"));
								$stringArrayJ = $jourAdd->format('d')."/".$jourAdd->format('m');
								//var_dump($event->getDate()->format ('U')." <= ".$jourAdd->format('U')." and ".$event->getDateFin()->format ('U')." > = ".$jourAdd->format('U'));
								if(($event->getDate()->format ('U') >= $jourAdd->format('U') and $event->getDateFin()->format ('U') <= $jourAddPlus->format('U')) or ($event->getDate()->format ('U') <= $jourAdd->format('U') and $event->getDateFin()->format ('U') >= $jourAdd->format('U')  ) or ($event->getDate()->format ('U') >= $jourAdd->format('U') and $event->getDate()->format ('U') <= $jourAddPlus->format('U')) ){
									//var_dump($stringArrayJ." ".$event->getTitle());	
									
									
									if(!isset($arrayJour[$stringArrayJ])){
										$arrayJour[$stringArrayJ] = 0;
									}
									
									$arrayJour[$stringArrayJ] += 1;
									
									
								}else{
									
									if(!isset($arrayJour[$stringArrayJ])){
										$arrayJour[$stringArrayJ] = 0;
									}
									//$arrayJour[$stringArrayJ] += 0;
								}
							}
						}
						
						
						$thread = $treadManager->findThreadById ( 'type[comment]_channel[event_' . $event->getId () . ']' );
						if ($thread) {
							// recup comment
							$comments = $commentManager->findCommentsByThread ( $thread );
							$numberComments += count ( $comments );
						}
						
						//var_dump($arrayJour);
						
						if ($type == "call") {
							$numberCall += 1;
							$numberCallPastTime = round(($event->getDateFin()->format ('U') - $event->getDate()->format ( 'U' ))/3600);
							$totalTime += $numberCallPastTime;
						} else if ($type == "event") {
							$numberEvent += 1;
							$numberEventPastTime = round(($event->getDateFin()->format ('U') - $event->getDate()->format ( 'U' ))/3600);
							$totalTime += $numberEventPastTime;
						} else if ($type == "meeting") {
							$numberTask += 1;
							$numberTaskPastTime = round(($event->getDateFin()->format ('U') - $event->getDate()->format ( 'U' ))/3600);
							$totalTime += $numberTaskPastTime;
						}
						
						
						$thread = $treadManager->findThreadById ( 'type[comment]_channel[event_' . $event->getId () . ']' );
						if ($thread) {
							// recup comment
							$comments = $commentManager->findCommentsByThread ( $thread );
							$numberComments += count ( $comments );
						}
						
						// participants
						
						$participants = $event->getParticipants ();
						$numberParticipants += count ( $participants );
						
						// participation
						foreach ( $participants as $participant ) {
							$participationState = $participant->getParticipationState ();
							
							if ($participationState == ParticipantEvent::PARTICIPATION_STATE_INVITED) {
								$numberInvited += 1;
							} else if ($participationState == ParticipantEvent::PARTICIPATION_STATE_DONTKNOW) {
								$numberDontKnow += 1;
							} else if ($participationState == ParticipantEvent::PARTICIPATION_STATE_PARTICIPATING) {
								$numberParticipating += 1;
							} else if ($participationState == ParticipantEvent::PARTICIPATION_STATE_CANCELED) {
								$numberCanceled += 1;
							}
						
						}
						$numberEvents += 1;
					
					}
				}
			}
		}
		
		
		$tailleEcran = $request->get ( "tailleecran" );
		if(empty($tailleEcran)){
			$tailleEcran = 400;
		}
		
		//var_dump($arrayJour);
			if($mobile == true)
				return $this->render ( 'IMTReportingBundle:Default:mobile.html.twig', array ("numberEvent" => $numberEvents, "numberComment" => $numberComments, "user" => $user, 'nbCall' => $numberCall, 'nbTask' => $numberTask, 'nbEvent' => $numberEvent, 'numberInvited' => $numberInvited, 'numberDontKnow' => $numberDontKnow, 'numberParticipating' => $numberParticipating, 'numberCanceled' => $numberCanceled, 'numberCallPastTime' => $numberCallPastTime, 'numberEventPastTime' => $numberEventPastTime, 'numberTaskPastTime' => $numberTaskPastTime, 'jourEvent' => $jourEvent, 'arrayJour' => $arrayJour, 'totalTime' => $totalTime, 'tailleEcran' => $tailleEcran  ) );
			else	
				return $this->render ( 'IMTReportingBundle:Default:index.html.twig', array ("numberEvent" => $numberEvents, "numberComment" => $numberComments, "user" => $user, 'nbCall' => $numberCall, 'nbTask' => $numberTask, 'nbEvent' => $numberEvent, 'numberInvited' => $numberInvited, 'numberDontKnow' => $numberDontKnow, 'numberParticipating' => $numberParticipating, 'numberCanceled' => $numberCanceled, 'numberCallPastTime' => $numberCallPastTime, 'numberEventPastTime' => $numberEventPastTime, 'numberTaskPastTime' => $numberTaskPastTime, 'jourEvent' => $jourEvent, 'arrayJour' => $arrayJour, 'totalTime' => $totalTime  ) );
	}
	
	
	public function mobileAction() {
		return $this->indexAction(true);
		//return $this->render ( 'IMTReportingBundle:Default:mobile.html.twig');
	}
}